package com.haochen.generation.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

@State(Scope.Thread)
public class JsonSerialize {
    private User user;
    private ObjectMapper mapper;
//    private Gson gson;

    private List<User> users;

    private String userJson;

    @Setup
    public void setup() {
        user = new User();
        user.setId(1L);
        user.setName("赵侠客");
        user.setAge(29);
        user.setSex("男");
        user.setTrueName("公众号");
        user.setCreateTime(new Date());

        users = new ArrayList<>();
        IntStream.range(1, 20).forEach(x -> {
            User user = new User();
            user.setId(1L);
            user.setName(RandomUtil.randomString("公众号:赵侠客",100));
            user.setAge(29);
            user.setSex("男");
            user.setTrueName(RandomUtil.randomString("公众号:赵侠客",100));
            user.setCreateTime(new Date());

            User sub = new User();
            sub.setId(1L);
            sub.setName(RandomUtil.randomString("公众号:赵侠客",100));
            sub.setAge(29);
            sub.setSex("男");
            sub.setTrueName(RandomUtil.randomString("公众号:赵侠客",100));
            sub.setCreateTime(new Date());
            user.setSub(sub);

            users.add(user);

            userJson = JSONUtil.toJsonStr(users);
        });

        mapper = new ObjectMapper();
//        gson = new Gson();
    }

//    @TearDown
//    public void tearDown() {
//        user = null;
//        mapper = null;
//        gson = null;
//    }

//    @Benchmark
//    public void testFastJson() {
//        String json = JSON.toJSONString(user);
//    }

    @Benchmark
    public void testFast2Json() {
        List<User> list  = com.alibaba.fastjson2.JSON.parseArray(userJson, User.class);
    }

    @Benchmark
    public void testHutoolJson() {
        List<User> list   = JSONUtil.toList(userJson, User.class);
    }

    @Benchmark
    public void testJackson() throws JsonProcessingException {
        List<User> list   = mapper.readValue(userJson, List.class);
    }

//    @Benchmark
//    public void testGson() {
//        String json = gson.toJson(user);
//    }

    @Test
    public void testBenchmark() throws Exception {
        org.openjdk.jmh.runner.options.Options options = new OptionsBuilder()
                .include(JsonSerialize.class.getSimpleName())
                .forks(1)
                .threads(Threads.MAX)
                .warmupIterations(3)
                .measurementIterations(3)
                .mode(Mode.Throughput)
                .build();
        new Runner(options).run();
    }
}

